/*     */ package com.chw.admin.login;
/*     */ 
/*     */ import com.chw.admin.user.ISysUserService;
/*     */ import com.chw.admin.user.SysUser;
/*     */ import com.chw.admin.user.SysUserVo;
/*     */ import com.chw.common.SecureInfo;
/*     */ import com.chw.common.ServiceData;
/*     */ import com.chw.common.ServiceData.RetCode;
/*     */ import com.chw.utils.LogUtil;
/*     */ import com.chw.utils.Md5EncryptUtil;
/*     */ import com.chw.utils.TimeUtil;
/*     */ import com.chw.utils.UUIDUtil;
/*     */ import com.google.common.base.Strings;
/*     */ import com.google.gson.Gson;
/*     */ import com.google.gson.JsonElement;
/*     */ import com.google.gson.JsonObject;
/*     */ import java.util.Date;
/*     */ import java.util.HashMap;
/*     */ import java.util.Map;
/*     */ import javax.servlet.http.HttpServletRequest;
/*     */ import javax.servlet.http.HttpSession;
/*     */ import org.springframework.beans.factory.annotation.Autowired;
/*     */ import org.springframework.stereotype.Controller;
/*     */ import org.springframework.web.bind.annotation.RequestMapping;
/*     */ import org.springframework.web.bind.annotation.ResponseBody;
/*     */ 
/*     */ @Controller
/*     */ public class LoginController
/*     */ {
/*  32 */   private static final LogUtil log = LogUtil.newInstance(LoginController.class);
/*     */ 
/*     */   @Autowired
/*     */   private ISysUserService sysUserService;
/*     */ 
/*     */   @Autowired
/*     */   private ISysUsersLoginLogService sysUsersLoginLogService;
/*     */ 
/*     */   @RequestMapping(value={"/login.do"}, method={org.springframework.web.bind.annotation.RequestMethod.POST}, consumes={"*/*"}, produces={"application/json"})
/*     */   @ResponseBody
/*     */   public ServiceData login(HttpServletRequest request) throws Exception {
/*  49 */     ServiceData ret = new ServiceData();
/*  50 */     ret.setCode(request, ServiceData.RetCode.Success);
/*  51 */     String username = request.getParameter("username");
/*  52 */     String password = request.getParameter("password");
/*     */ 
/*  54 */     String sessionCheckcode = (String)request.getSession().getAttribute("randomstr");
/*  55 */     request.getSession().removeAttribute("randomstr");
/*  56 */     password = password.trim();
/*     */ 
/*  58 */     String remoteAddr = request.getRemoteAddr();
/*     */ 
/*  60 */     SysUsersLoginLog sysUsersLoginLog = new SysUsersLoginLog();
/*  61 */     sysUsersLoginLog.setLoginUserName(username);
/*  62 */     sysUsersLoginLog.setEnabledFlag("N");
/*  63 */     SysUsersLoginLog sysUsersLoginLogInsert = new SysUsersLoginLog();
/*  64 */     sysUsersLoginLogInsert.setUsersLoginLogId(UUIDUtil.getUUID());
/*  65 */     sysUsersLoginLogInsert.setLoginIp(remoteAddr);
/*  66 */     sysUsersLoginLogInsert.setLoginTime(new Date());
/*  67 */     sysUsersLoginLogInsert.setLoginUserName(username);
/*  68 */     sysUsersLoginLogInsert.setEnabledFlag("N");
/*     */ 
/*  71 */     if (Strings.isNullOrEmpty(username)) {
/*  72 */       ret.setBo(Integer.valueOf(3));
/*  73 */       sysUsersLoginLogInsert.setLoginStatus(Integer.valueOf(110));
/*  74 */       sysUsersLoginLogInsert.setErrorMessage("用户名为空");
/*  75 */       this.sysUsersLoginLogService.saveSysUsersLoginLog(sysUsersLoginLogInsert);
/*  76 */       return ret;
/*     */     }
/*  78 */     if (Strings.isNullOrEmpty(password)) {
/*  79 */       ret.setBo(Integer.valueOf(4));
/*  80 */       sysUsersLoginLogInsert.setLoginStatus(Integer.valueOf(120));
/*  81 */       sysUsersLoginLogInsert.setErrorMessage("密码为空");
/*  82 */       this.sysUsersLoginLogService.saveSysUsersLoginLog(sysUsersLoginLogInsert);
/*  83 */       return ret;
/*     */     }
/*     */ 
/* 100 */     password = Md5EncryptUtil.encode(password, true);
/* 101 */     username = username.trim();
/* 102 */     SysUser sysUser = this.sysUserService.selectByAccount(username);
/* 103 */     if (sysUser != null) {
/* 104 */       int erc = sysUser.getErrorCount();
/* 105 */       String thisDate = TimeUtil.formatDate(new Date(), "yyyy-MM-dd");
/* 106 */       if (!thisDate.equals(sysUser.getLoginDate())) {
/* 107 */         erc = 0;
/*     */       }
/* 109 */       if ((erc >= 5) && (thisDate.equals(sysUser.getLoginDate()))) {
/* 110 */         sysUsersLoginLogInsert.setLoginStatus(Integer.valueOf(140));
/* 111 */         sysUsersLoginLogInsert.setErrorMessage("错误次数超过限制");
/* 112 */         this.sysUsersLoginLogService.saveSysUsersLoginLog(sysUsersLoginLogInsert);
/* 113 */         ret.setBo(Integer.valueOf(10));
/* 114 */         return ret;
/*     */       }
/* 116 */       String passwordDb = sysUser.getPassword();
/* 117 */       if (password.equals(passwordDb)) {
/* 118 */         SysUserVo suv = new SysUserVo();
/* 119 */         suv.setAccount(sysUser.getAccount());
/* 120 */         suv.setEmail(sysUser.getEmail());
/* 121 */         suv.setId(sysUser.getId());
/* 122 */         suv.setMobile(sysUser.getMobile());
/* 123 */         suv.setOrgId(sysUser.getOrgId());
/* 124 */         suv.setOrgName(sysUser.getOrgName());
/* 125 */         suv.setSecureToken(new SecureInfo(sysUser.getId()).encode());
/* 126 */         suv.setUsername(sysUser.getUsername());
/* 127 */         suv.setBelongToScene(sysUser.getBelongToScene());
/* 128 */         request.getSession().setAttribute("account", suv);
/* 129 */         ret.setBo(Integer.valueOf(0));
/* 130 */         log.info("login--id>" + request.getRemoteHost() + "--username-->" + username + "登录系统");
/* 131 */         sysUsersLoginLogInsert.setLoginStatus(Integer.valueOf(100));
/* 132 */         sysUsersLoginLogInsert.setErrorMessage("登录成功");
/* 133 */         this.sysUsersLoginLogService.saveSysUsersLoginLog(sysUsersLoginLogInsert);
/*     */ 
/* 135 */         SysUser sysUserNew = new SysUser();
/* 136 */         sysUserNew.setId(sysUser.getId());
/* 137 */         sysUserNew.setErrorCount(0);
/* 138 */         sysUserNew.setLoginDate(thisDate);
/* 139 */         this.sysUserService.saveOrUpdate(sysUserNew);
/*     */       } else {
/* 141 */         ret.setBo(Integer.valueOf(2));
/* 142 */         sysUsersLoginLogInsert.setLoginStatus(Integer.valueOf(120));
/* 143 */         sysUsersLoginLogInsert.setErrorMessage("密码错误");
/* 144 */         this.sysUsersLoginLogService.saveSysUsersLoginLog(sysUsersLoginLogInsert);
/* 145 */         Map other = new HashMap();
/* 146 */         other.put("count", Integer.valueOf(erc + 1));
/* 147 */         ret.setOther(other);
/*     */ 
/* 149 */         SysUser sysUserNew = new SysUser();
/* 150 */         sysUserNew.setId(sysUser.getId());
/* 151 */         sysUserNew.setErrorCount(erc + 1);
/* 152 */         sysUserNew.setLoginDate(thisDate);
/* 153 */         this.sysUserService.saveOrUpdate(sysUserNew);
/*     */       }
/*     */     }
/*     */     else {
/* 157 */       ret.setBo(Integer.valueOf(2));
/* 158 */       Map other = new HashMap();
/* 159 */       other.put("count", Integer.valueOf(1));
/* 160 */       ret.setOther(other);
/* 161 */       sysUsersLoginLogInsert.setLoginStatus(Integer.valueOf(120));
/* 162 */       sysUsersLoginLogInsert.setErrorMessage("用户名错误");
/* 163 */       this.sysUsersLoginLogService.saveSysUsersLoginLog(sysUsersLoginLogInsert);
/*     */     }
/* 165 */     return ret;
/*     */   }
/*     */ 
/*     */   @RequestMapping(value={"/logout.do"}, method={org.springframework.web.bind.annotation.RequestMethod.POST}, consumes={"*/*"}, produces={"application/json"})
/*     */   @ResponseBody
/*     */   public ServiceData logout(HttpServletRequest request)
/*     */     throws Exception
/*     */   {
/* 178 */     log.info("-========->退出系统");
/* 179 */     SysUserVo su = (SysUserVo)request.getSession().getAttribute("account");
/* 180 */     if (su != null) {
/* 181 */       log.info("username-->" + su.getAccount() + "退出系统");
/*     */     }
/* 183 */     request.getSession().invalidate();
/* 184 */     request.getSession().removeAttribute("account");
/* 185 */     ServiceData ret = new ServiceData();
/* 186 */     ret.setCode(request, ServiceData.RetCode.Success);
/* 187 */     ret.setBo(Integer.valueOf(0));
/* 188 */     return ret;
/*     */   }
/*     */ 
/*     */   @RequestMapping(value={"/checklogin.do"}, method={org.springframework.web.bind.annotation.RequestMethod.POST}, consumes={"*/*"}, produces={"application/json"})
/*     */   @ResponseBody
/*     */   public ServiceData checklogin(HttpServletRequest request)
/*     */     throws Exception
/*     */   {
/* 201 */     Object obj = request.getSession().getAttribute("account");
/* 202 */     ServiceData ret = new ServiceData();
/* 203 */     ret.setCode(request, ServiceData.RetCode.Success);
/* 204 */     ret.setBo(Integer.valueOf(1));
/* 205 */     if (obj != null) {
/* 206 */       JsonObject jsonObject = new Gson().toJsonTree(obj).getAsJsonObject();
/* 207 */       String myId = jsonObject.get("id").toString();
/* 208 */       if ((myId != null) && (myId.length() > 0)) {
/* 209 */         if (myId.startsWith("\"")) {
/* 210 */           myId = myId.substring(1);
/*     */         }
/* 212 */         if (myId.endsWith("\"")) {
/* 213 */           myId = myId.substring(0, myId.length() - 1);
/*     */         }
/*     */       }
/* 216 */       if ((myId == null) || ("".equals(myId))) {
/* 217 */         ret.setBo(Integer.valueOf(0));
/*     */       }
/*     */     }
/* 220 */     return ret;
/*     */   }
/*     */ }

/* Location:           E:\chenya\2017 4-21\com.chw\admin\login\
 * Qualified Name:     com.chw.admin.login.LoginController
 * JD-Core Version:    0.6.0
 */